
/**
 * @Author: 陈瑞鹏
 * @Date:   2022-10-21 11:24:29
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2022-10-28 19:06:55
 */
import abiao_utils from "abiao_utils";
import AchTask from "../models/task/achTask.model";
import DailyTask from "../models/task/dailyTask.model";
import Integral from "../models/task/integral.model";
import RandTask from "../models/task/randTask.model";
import bigNumber from "../utils/bigNumber";
import utils from "../utils/utils";

export default class task {

  /**
   * 成就任务初始化
   * @param roleId 
   */
  static async achTaskInit(roleId: number) {
    let achTaskData: any = await utils.get_all_json("task/task.json");
    for (const interator of achTaskData) {
      let achTaskRes: any = await AchTask.findOne({ roleId, id: interator.id });
      if (!achTaskRes) {
        let info = {
          roleId,
          id: interator.id,
          name: interator.name,
        }
        await AchTask.create(info);
      }
    }
  }

  /**
   * 每日任务初始化
   * @param roleId 
   */
  static async dailyTaskInit(roleId: number) {
    let dailyTaskData: any = await utils.get_all_json("task/dailyTask.json");
    for (const interator of dailyTaskData) {
      let DailyTaskRes: any = await DailyTask.findOne({ roleId, id: interator.id });
      if (!DailyTaskRes) {
        let info = {
          roleId,
          id: interator.id,
          name: interator.name,
          rewardType: interator.rewardType,
          rewardNum: interator.rewardNum
        }
        await DailyTask.create(info);
      } else {
        let date = abiao_utils.date(2);
        if (date != DailyTaskRes.updateTime) {
          await DailyTask.updateOne({ roleId, id: interator.id }, { target: interator.target, rewardType: interator.rewardType, rewardNum: interator.rewardNum, progress: 0, isReceive: false, updateTime: abiao_utils.date(2) });
        }
      }
    }
  }

  /**
    * 随机任务初始化
    * @param roleId 角色ID
    */
  static async randTaskInit(roleId: number) {
    let randTaskData: any = await utils.get_all_json('task/randTask.json');
    for (const iterator of randTaskData) {
      let RandTaskRes: any = await RandTask.findOne({ roleId, id: iterator.id });
      if (!RandTaskRes) {
        let info = {
          roleId,
          id: iterator.id,
          name: iterator.name,
          target: iterator.target,
        }
        await RandTask.create(info);
      } else {
        await RandTask.updateOne({ roleId, id: iterator.id }, { target: iterator.target });
      }
    }
  }

  /**
     * 成就点数初始化
     * @param roleId 角色ID
     */
  static async integralInit(roleId: number) {
    let IntegralRes: any = await Integral.findOne({ roleId });
    let IntegralData: any = await utils.get_all_json('task/integral.json');
    if (!IntegralRes) {

      for (const iterator of IntegralData) {
        iterator.isReceive0 = false;
        iterator.isReceive1 = false;
      }

      let info = {
        roleId,
        reward: IntegralData
      }
      await Integral.create(info);
    } else {
      for (const iterator of IntegralRes.reward) {
        for (const item of IntegralData) {
          if (iterator.target == item.target) {
            iterator.reward = item.reward;
          }
        }
      }
      await Integral.updateOne({ roleId }, { reward: IntegralRes.reward })
    }
  }

  /**
  * 完成成就任务
  * @param roleId 角色ID
  * @param id 任务ID
  * @param progress 进度
  * @returns
  */
  static async complete_ach_task(roleId: number, id: number, progress: number) {
    let AchTaskRes: any = await AchTask.findOne({ roleId, id });
    if (!AchTaskRes) return

    let nowProgress = bigNumber.jia(AchTaskRes.progress, progress);
    await AchTask.updateOne({ roleId, id }, { progress: nowProgress });
  }

  /**
   * 完成每日任务
   * @param roleId 角色ID
   * @param id 任务ID
   * @param progress 进度
   * @returns
   */
  static async complete_daily_task(roleId: number, id: number, progress: number) {
    let DailyTaskRes: any = await DailyTask.findOne({ roleId, id });
    if (!DailyTaskRes) return

    let nowProgress = bigNumber.jia(DailyTaskRes.progress, progress);
    await DailyTask.updateOne({ roleId, id }, { progress: nowProgress });
  }
}